# 神经元
import numpy as np


def active_function(x):
    """【激活函数】S型函数"""
    y = sigmoid(x)  # 如有需要，直接更换此处代码以简单地更换激活函数
    return y


def sigmoid(x):
    """【函数】S型函数"""
    return (1 / (1 + np.exp(-x)))


def deriv_sigmoid(x):
    """【函数】S型函数的导数：f(x)·[1-f(x)]"""
    # fx = sigmoid(x)
    # y = fx*(1-fx)
    return (sigmoid(x) * (1-sigmoid(x)))


def mse_loss(y_ture: np.ndarray, y_pred: np.ndarray):
    """【函数】平均方差（MSE）损失"""
    mse = ((y_ture - y_pred)**2).mean()
    return mse


class Neuron():
    """【类】神经元"""

    def __init__(self, FP_weights: list, FP_bias: float) -> None:
        """【函数】初始化"""
        self.weights_vct = np.array(FP_weights)
        self.bias = FP_bias
        return None

    def feedforward(self, FP_inputs: list):
        """【函数】前馈计算"""
        self.inputs_vct = np.array(FP_inputs)
        x = np.dot(self.inputs_vct, self.weights_vct) + self.bias
        return active_function(x)
    
    
    


# test
def test_neuron():
    w = (2, 3)
    x = (0, 1)
    b = 4

    neuron_1 = Neuron(w, b)
    y = neuron_1.feedforward(x)
    print("神经元前馈:", y)
    print("对比:", (1 / (1 + np.exp(-(x[0]*w[0]+x[1]*w[1]+b)))))
    return None


def test_network():
    w = (0, 1)
    x = (2, 3)
    b = 0

    hln_1 = Neuron(w, b)
    hln_2 = Neuron(w, b)

    h1 = hln_1.feedforward(x)
    h2 = hln_2.feedforward(x)

    h = (h1, h2)
    oln = Neuron(w, b)
    y = oln.feedforward(h)
    print("神经元前馈:", y)
    return None


if (__name__ == "__main__"):
    test_neuron()
    test_network()